
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lansupportserver;

import ObjectHandler.LanSupportMessage;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Quan
 */
public class RemoteDesktop extends javax.swing.JFrame {
    
    private Socket sendSocket;

    /**
     * Creates new form RemoteDesktop
     */
    public RemoteDesktop(Socket sendSocket) {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.sendSocket = sendSocket;
        MyKeyListener myKeyListener = new MyKeyListener(sendSocket);
        this.addKeyListener(myKeyListener);
    }
    
    private void StopRemote() {
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.StopRemoteDesktop);
        lanSupportMessage.Send(sendSocket);
    }
    
    public void setWindowSize(byte[] bs) {
        byte[] xx = new byte[8];
        byte[] yy = new byte[8];
        System.arraycopy(bs, 0, xx, 0, 8);
        System.arraycopy(bs, 8, yy, 0, 8);
        int x = (int) _bytes2Long(xx);
        int y = (int) _bytes2Long(yy);
        this.setSize(x, y);
        this.setLocationRelativeTo(null);
    }
    
    public void changeImage(byte[] imageData) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
            Iterator<?> readers = ImageIO.getImageReadersByFormatName("png");
            ImageReader reader = (ImageReader) readers.next();
            Object source = bis; //Returns an ImageInputStream that will take its input from the given Object
            ImageInputStream iis = ImageIO.createImageInputStream(source);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Image image = reader.read(0, param);
            jPanel1.setImg(image);
        } catch (IOException ex) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new ImagePanel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                jPanel1MouseWheelMoved(evt);
            }
        });
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanel1MouseReleased(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel1MouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanel1MousePressed(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel1MouseExited(evt);
            }
        });
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanel1MouseMoved(evt);
            }
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanel1MouseDragged(evt);
            }
        });
        jPanel1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jPanel1KeyReleased(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPanel1KeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 406, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 306, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MousePressed
        LanSupportMessage lanSupportMessage = null;
        int button = evt.getButton();
        switch (button) {
            case 1:
                lanSupportMessage = new LanSupportMessage(LanSupportMessage.LeftMouseClick);
                break;
            case 2:
                lanSupportMessage = new LanSupportMessage(LanSupportMessage.WheelMouseClick);
                break;
            case 3:
                lanSupportMessage = new LanSupportMessage(LanSupportMessage.RightMouseClick);
                break;
        }
        byte[] point = new byte[16];
        System.arraycopy(_long2Bytes(evt.getX()), 0, point, 0, 8);
        System.arraycopy(_long2Bytes(evt.getY()), 0, point, 8, 8);
        lanSupportMessage.setData(point);
        byte[] xx = new byte[8];
        byte[] yy = new byte[8];
        System.arraycopy(point, 0, xx, 0, 8);
        System.arraycopy(point, 8, yy, 0, 8);
        lanSupportMessage.Send(sendSocket);
    }//GEN-LAST:event_jPanel1MousePressed
    
    private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseReleased
        LanSupportMessage lanSupportMessage = null;
        int button = evt.getButton();
        switch (button) {
            case 1:
                lanSupportMessage = new LanSupportMessage(LanSupportMessage.MouseRelease);
                break;
            case 2:
                lanSupportMessage = new LanSupportMessage(LanSupportMessage.WheelMouseRelease);
                break;
            case 3:
                lanSupportMessage = new LanSupportMessage(LanSupportMessage.RightMouseRelease);
                break;
        }
        byte[] point = new byte[16];
        System.arraycopy(_long2Bytes(evt.getX()), 0, point, 0, 8);
        System.arraycopy(_long2Bytes(evt.getY()), 0, point, 8, 8);
        lanSupportMessage.setData(point);
        byte[] xx = new byte[8];
        byte[] yy = new byte[8];
        System.arraycopy(point, 0, xx, 0, 8);
        System.arraycopy(point, 8, yy, 0, 8);
        lanSupportMessage.Send(sendSocket);
    }//GEN-LAST:event_jPanel1MouseReleased
    
    private void jPanel1MouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_jPanel1MouseWheelMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel1MouseWheelMoved
    
    private void jPanel1MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseMoved
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.MouseMoved);
        byte[] point = new byte[16];
        System.arraycopy(_long2Bytes(evt.getX()), 0, point, 0, 8);
        System.arraycopy(_long2Bytes(evt.getY()), 0, point, 8, 8);
        lanSupportMessage.setData(point);
        byte[] xx = new byte[8];
        byte[] yy = new byte[8];
        System.arraycopy(point, 0, xx, 0, 8);
        System.arraycopy(point, 8, yy, 0, 8);
        lanSupportMessage.Send(sendSocket);
    }//GEN-LAST:event_jPanel1MouseMoved
    
    private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseDragged
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.MouseMoved);
        byte[] point = new byte[16];
        System.arraycopy(_long2Bytes(evt.getX()), 0, point, 0, 8);
        System.arraycopy(_long2Bytes(evt.getY()), 0, point, 8, 8);
        lanSupportMessage.setData(point);
        byte[] xx = new byte[8];
        byte[] yy = new byte[8];
        System.arraycopy(point, 0, xx, 0, 8);
        System.arraycopy(point, 8, yy, 0, 8);
        lanSupportMessage.Send(sendSocket);
    }//GEN-LAST:event_jPanel1MouseDragged
    
    private void jPanel1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel1MouseEntered
    
    private void jPanel1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseExited
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel1MouseExited
    
    private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPanel1KeyPressed
        JOptionPane.showMessageDialog(rootPane, evt.getKeyCode());
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.keyPress);
        byte[] KeyCode = _long2Bytes(evt.getKeyCode());
        lanSupportMessage.setData(KeyCode);
        lanSupportMessage.Send(sendSocket);
        System.out.println(String.valueOf(KeyCode));
    }//GEN-LAST:event_jPanel1KeyPressed
    
    private void jPanel1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPanel1KeyReleased
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.keyRelease);
        byte[] KeyCode = _long2Bytes(evt.getKeyCode());
        lanSupportMessage.setData(KeyCode);
        lanSupportMessage.Send(sendSocket);
        System.out.println(String.valueOf(KeyCode));
    }//GEN-LAST:event_jPanel1KeyReleased
    
    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        StopRemote();
    }//GEN-LAST:event_formWindowClosed
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        StopRemote();
    }//GEN-LAST:event_formWindowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ImagePanel jPanel1;
    // End of variables declaration//GEN-END:variables

    private byte[] _long2Bytes(long l) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        byteBuffer.putLong(l);
        byte[] byteResult = byteBuffer.array();
        return byteResult;
    }
    
    private long _bytes2Long(byte[] bs) {
        long value = 0;
        for (int i = 0; i < bs.length; i++) {
            byte c = bs[i];
            value = (value << 8) + (c & 0xff);
        }
        return value;
    }
    
    public void SendRequest() {
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.StartRemoteDesktop);
        lanSupportMessage.Send(sendSocket);
    }
}
